<?php

class user extends Model
{

	function getSiteAdmin() {
		return $this->db->getAll(
			"SELECT stud_no 
			FROM user
			WHERE admin = 1
			"
		);	
	}

	function getAllUsers() {
		return $this->db->getAll(
		"SELECT * 
		FROM user
		WHERE admin = 0"
		);
	}
	
	function getAllProfessors() {
		return $this->db->getAll(
			"SELECT
				stud_no,
				lastname,
				firstname,
				middlename
			FROM
				user
			WHERE
				(lastname LIKE ?
				OR
					firstname LIKE ?
				OR stud_no LIKE ?
				OR CONCAT(firstname,' ',lastname) LIKE ?)
			AND admin = 2
				",
			array( "%$name%", "%$name%","%$name%", "%$name%" )
		);
	}

	function findUser( $stud_no ) {
		return $this->db->getAll(
			"SELECT 
				stud_no,
				password,
				firstname,
				lastname,
				middlename,
				admin,
				email
			FROM 
				user
			WHERE
				stud_no = ?",
			array( $stud_no )
		);
	}
	
	function findUserByNameOrNumber( $name ) {
		return $this->db->getAll(
			"SELECT 
				stud_no,
				lastname,
				firstname
			FROM 
				user
			WHERE
				lastname LIKE ?
			OR
				firstname LIKE ?
			OR stud_no LIKE ?
			OR CONCAT(firstname,' ',lastname) LIKE ?
				",
			array( "%$name%", "%$name%","%$name%", "%$name%" )
		);
	}
	
	function findUserByName( $name ) {
		return $this->db->getAll(
			"SELECT 
				stud_no,
				lastname,
				firstname
			FROM 
				user
			WHERE
				lastname LIKE ?
			OR
				firstname LIKE ?
			OR CONCAT(firstname,' ',lastname) LIKE ?
				",
			array( "%$name%", "%$name%", "%$name%" )
		);
	}

	function getInactiveUsers() {
		return $this->db->getAll( 
			"SELECT 
				stud_no,
				lastname,
				firstname
			FROM 
				user
			WHERE
				stud_no = ?",
			array( $stud_no )
		);
	}
	
	function insertUserComplete($stud_no, $password, $lname, $fname, $mname, $admin, $email)
	{
		$iuser = $this->db->execute("INSERT INTO user( 
			stud_no, 
			password,
			lastname,
			firstname,
			middlename,
			admin,
			email
			)
			VALUES ( ?, ?, ?, ?, ?, ?, ? )", 
			array( $stud_no, $password, $lname, $fname, $mname, $admin, $email )
		);
		return $iuser;
	}
	
	function insertUser($stud_no, $lname, $fname, $mname)
	{
		$iuser = $this->db->execute("INSERT INTO user( stud_no, 
			lastname,
			firstname,
			middlename )
			VALUES ( ?, ?, ?, ? )", 
			array( $stud_no, $lname, $fname, $mname )
		);
		return $iuser;
	}

	function updateUserComplete( $stud_no, $lastname, $firstname, $middlename, $password, $admin, $email ) 
	{
		$uuser = $this->db->execute("UPDATE user
			SET lastname = ?,
				firstname = ?,
				middlename = ?,
				password = ?,
				admin = ?,
				email= ?
			WHERE stud_no = ?",
			array( $lastname, $firstname, $middlename, $password, $admin, $email, $stud_no )
		);
		return $uuser;
	}
	
	function updateUser( $lastname, $firstname, $middlename, $stud_no ) 
	{
		$uuser = $this->db->execute("UPDATE user
			SET lastname = ?,
				firstname = ?,
				middlename = ?
			WHERE stud_no = ?",
			array( $lname, $fname, $mname, $stud_no )
		);
		return $uuser;
	}

	function deleteUser ( $stud_no )
	{
		$duser = $this->db->execute("DELETE FROM user
			WHERE stud_no = ? ",
			array( $stud_no )
		);
	}
	
	function logIn ( $stud_no, $password )
	{
		$login = $this->db->getAll("SELECT user.stud_no, 
				user.firstname, 
				user.lastname, 
				user.middlename,
				user.admin
			FROM user
			WHERE user.stud_no = ?
			AND user.password = ?",
			array( $stud_no, $password )	
		);
		return $login;
	}

	function changePassword($stud_no, $oldpass, $newpass) {
		return $this->db->execute(
			"UPDATE user
			SET password = ?
			WHERE stud_no = ? AND password = ?",
			array($newpass, $stud_no, $oldpass)
		);
	}
	
}